"""gitcode接口调用"""
import inspect
from functools import wraps
from .base import BaseClient, api
from src.common.environment import GitCodeEnv
from src.common.config import PROXIES

TOKEN = GitCodeEnv.GITCODE_TOKEN


def blue_auth():
    def decorate(func):

        @wraps(func)
        def wrapper(self, *arg, **kwargs):
            func_params = inspect.signature(func).bind(self, *arg, **kwargs)
            func_params.apply_defaults()
            query_params = func_params.arguments.get('params')
            password = TOKEN
            if query_params is None:
                kwargs['params'] = {'access_token': password}
            else:
                query_params['access_token'] = password
            return func(self, *arg, **kwargs)

        return wrapper

    return decorate


class GitCodeClient(BaseClient):
    DOMAIN = GitCodeEnv.GITCODE_API_URL
    PROXIES = PROXIES

    @blue_auth()
    @api(method='get', route='/api/v5/repos/{repo_owner}/{repo_name}/issues')
    def get_repo_issues(self, repo_owner, repo_name, params=None):
        """获取仓库所有issues"""
